package com.djsearch.ui;

import java.util.ArrayList;
import java.util.List;

import yqf.Jjl;
import yqf.Query;
import yqf.YqfAD;
import yqf.data.YiqifaAD;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.djsearch.R;
import com.djsearch.bean.GroupBuyGoods;
import com.djsearch.controller.JSONResp;
import com.djsearch.data.CommunicationConstants;
import com.djsearch.widget.MyWebChromeClient;
import com.djsearch.widget.MyWebView;

/**
 * 该页面是用来通过商家的商品url来查看详细的商品
 * 
 * @author chenyang2
 * 
 */
public class WebViewActivity extends BaseActivity {

	private Context mContext;
	private MyWebView webView;
	private String url;
	private Button backBtn;
    private String whichBuyType;
    private String lastUrl;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.webview_layout);
		mContext = this;
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		if (bundle != null) {
			url = bundle.getString("url");
			whichBuyType = bundle.getString("whichBuyType");
		}
		if (whichBuyType.equals("groupShop")) {
			lastUrl = CommunicationConstants.SHOP_GROUP_URL + "userId" + "&t=" ;
		}
		// 初始化主界面
		initView();
		// 初始化主界面相关点击事件
		initOnClick();
		//
		initHandler();
		
		
		
		YqfAD yqf = new YqfAD();//实例化广告获取主类
		Query q = new Query();//实例化广告获取参数类
		q.setCost("cps");//设置参数，广告位cps类型的
		YiqifaAD ad = yqf.getYiqifaAd(q,mContext);//实例化一个ad类
//		List list = (ADBean)ad.getAdbeans();//获取到返回的广告的list
        ad.getAdbeans();
		
		
		
		
		
		
	}

	@Override
	void initHandler() {
		// TODO Auto-generated method stub

	}

	@Override
	void initView() {
		// TODO Auto-generated method stub
		backBtn = (Button) findViewById(R.id.backBtn);
		webView = (MyWebView) findViewById(R.id.detailWebView);
		setWebView(webView);
	}

	@Override
	void initOnClick() {
		// TODO Auto-generated method stub
		backBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(mContext, GroupShopDetailsActivity.class, 1);
			}
		});
	}

	@Override
	void getDataToResolve(JSONResp response) {
		// TODO Auto-generated method stub

	}

	private void setWebView(MyWebView webView) {
		// if(m_doctype==null||m_doctype.equals("1"))//
		// {
		webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);//
		// }
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setSaveFormData(false);
		webView.getSettings().setSavePassword(false);
		webView.getSettings().setSupportZoom(false);
		final ProgressBar prgProgressBar = (ProgressBar) findViewById(R.id.progressBarOne);
		prgProgressBar.setProgress(10);
		webView.setWebChromeClient(new MyWebChromeClient() {

			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				// TODO Auto-generated method stub
				// TextView progress=(TextView)findViewById(R.id.progressText);
				// progress.setVisibility(View.VISIBLE);
				// progress.setText(newProgress+"%");
				// ProgressBar
				// progressbar=(ProgressBar)findViewById(R.id.progressBar);
				// progressbar.setVisibility(View.VISIBLE);
//				ProgressBar prgProgressBar = (ProgressBar) findViewById(R.id.progressBarOne);
				prgProgressBar.setVisibility(View.VISIBLE);
				prgProgressBar.setProgress(newProgress);

				if (newProgress == 100) {
					// progress.setVisibility(View.GONE);
					// progressbar.setVisibility(View.GONE);
					prgProgressBar.setVisibility(View.INVISIBLE);
				}
			}

		});
		// webView.addJavascriptInterface(new JavaScriptInterface(m_Handler),
		// "NewsWebJSInterface");
		webView.setWebViewClient(new WebViewClient() {

			public void onPageFinished(WebView view, String url) {

			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// TODO Auto-generated method stub
//				 Jjl jjl = new Jjl(mContext);
//				 String urlFromYiQiFa = jjl.filterTargetUrl(url) ;
//				Jjl jil = new Jil();
				// String urlFromYiQiFa = jjl.filterTargetUrl(url) ;
				 Jjl jjl = new Jjl(mContext);
//				 String urlSet = jjl.filterTargetUrl(url,4);
				 //http://i.meituan.com/zhengzhou/deal/651744.html
				 String urls = jjl.filterTargetUrl(url);
//				 String ret = jjl.filterTargetUrl(url,596767);

//				Log.i("WEB", url);
				return super.shouldOverrideUrlLoading(view, lastUrl + url);
			}
		});
		webView.getSettings().setDefaultTextEncodingName("UTF-8");
		webView.loadUrl(url);
	}

	/**
	 * 点击返回键
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
			webView.goBack();
			return true;
		} else if ((keyCode == KeyEvent.KEYCODE_BACK) && !webView.canGoBack()) {
			startActivity(mContext, GroupShopDetailsActivity.class, 1);
		}

		return super.onKeyDown(keyCode, event);
	}

	// Intent phoneIntent = new Intent("android.intent.action.DIAL",
	// Uri.parse("tel:" + number));
	//
	// startActivity(phoneIntent);
}
